package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Product;
import com.blb.entity.User;
import com.blb.entity.Ware;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IWareService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
@Controller
@RequestMapping("/warehouse")
public class WareController {
    @Resource
    private IWareService iwareService;

    @RequestMapping("{page}")
    public String wel(@PathVariable String page){
        return "warehouse/"+ page;
    }

    /* 分页列表和模糊查询 */
    @ResponseBody
    @RequestMapping("ware")
    public BlbPage ware( HttpSession session, String wareName,String deteTime, int start, int length){

        /*查询分页数据*/
        IPage page = iwareService.page(new Page(start, length), new QueryWrapper<Ware>().eq("company_id", ShiroUtils.getUser().getCompanyId()).like("ware_name", wareName).like("dete_time", deteTime));
        System.out.println(page.getRecords());

        /*查询总条数*/
        int count = iwareService.count(new QueryWrapper<Ware>().eq("company_id", ShiroUtils.getUser().getCompanyId()).like("ware_name",wareName).like("dete_time", deteTime));
        System.out.println(count);
        return new DataTablePage(count,count,page.getRecords());

    }

    /* 添加仓库 */
    @RequestMapping("addware")
    public String AddWare(@RequestBody Ware data,HttpSession session){
        data.setId(UUIDUtils.getId());
        /* 插入公司id */
        data.setStatus("0");
        data.setCompanyId(ShiroUtils.getUser().getCompanyId());
        iwareService.AddWare(data);
        return "warehouse/warehouse_manage";
    }

    /* 修改仓库 */
    @RequestMapping("updateware")
    public String UpdateWare(@RequestBody Ware data){
        iwareService.UpdateWare(data);
        return "warehouse/warehouse_manage";
    }

    /* 根据公司id查询仓库 */
    @ResponseBody
    @RequestMapping("findallware")
    public ResponseHelp findAllWare2(){
        List<Ware> allWare2 = iwareService.findAllWare2(ShiroUtils.getUser().getCompanyId());
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allWare2);
        return responseHelp;
    }

    /* 修改仓库启用状态 */
    @RequestMapping("updatewarestatus0")
    public String UpdateWareStatus(@RequestBody Ware data){

        iwareService.UpdateWareStatus(data);
        return "warehouse/warehouse_manage";
    }

    /* 修改仓库停用状态 */
    @RequestMapping("updatewarestatus1")
    public String UpdateWareStatus1(@RequestBody Ware data){
        iwareService.UpdateWareStatus1(data);
        return "warehouse/warehouse_manage";
    }

    /**
     * 采购列表所需（查找本公司的仓库）
     * xuyuanxin
     * @return
     */
    @ResponseBody
    @RequestMapping("allWareName")
    public ResponseHelp findWareName(){
        User user = ShiroUtils.getUser();
        List<Ware> allWare = iwareService.findAllWare(user.getCompanyId());
        System.out.println(allWare);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allWare);
        return responseHelp;
    }

    /**
     * 采购列表所需（查找仓库对应的负责人）
     * xuyuanxin
     * @return
     */
    @ResponseBody
    @RequestMapping("WarePerson")
    public ResponseHelp findWarePerson(String id){
        Ware byId = iwareService.getById(id);
        String person = byId.getPerson();
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(person);
        return responseHelp;
    }

}

